import Mock from 'mockjs';

// 生成产品数据
const generateProducts = Mock.mock({
    'list|30': [
        {
            'id|+1': 1000,
            'name': '@ctitle(3, 6)',
            'description': '@csentence(20, 50)',
            'price|50-5000': 100,
            'stock|0-100': 50,
            'category': '@pick(["电子", "家居", "服饰", "食品"])',
            'rating|1-5': 1,
            'image': '@image("200x200", "#4A7BF7", "Product")'
        }
    ]
}).list;

export default function(mock) {
    // 获取产品列表
    mock.onGet('/products').reply(config => {
        const { category } = config.params;
        const products = category
            ? generateProducts.filter(p => p.category === category)
            : generateProducts;

        return [200, {
            code: 200,
            data: products
        }];
    });
}